﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RVBike
{
    /* This class collects common definitions using in network environment
     * 
     * Includes :
     *   - Server port number
     *   - Client port number
     *   - Multicast port number
     *   - Command strings
     */
    class RVNetworkEnv
    {
        // Ports used in our application
        public const int SERVER_PORT     = 50001;
        public const int CLIENT_PORT     = 50002;
        public const int MULTICAST_PORT  = 50003;

        // Commands
        public const string QUERY_ROOM_INFO = "QRI";
        public const string ROOM_INFO = "RI";
        public const string JOIN_ROOM = "JOIN";
        public const string LEAVE_ROOM = "LEAVE";
        public const string PING = "PING";
        public const string UPDATE_STATE = "UP";
        public const string UPDATE_VIDEO = "UV";
        public const string UPDATE_AUDIO = "UA";
        public const string REPLY = "RP";
        public const string QUIT = "QT";
        public const string GAME_START = "GS";
        public const string START_PLAY = "SP";

        // Tags
        public const string CLIENT_IP = "CIP";
        public const string SERVER_IP = "SIP";
        public const string MCAST_IP = "MIP";
        public const string ROOM_NAME = "RN";
        public const string MAP_NAME = "MN";
        public const string GAME_MODE = "MD";
        public const string CURRENT_USER_COUNT = "CU";
        public const string MAX_USER_COUNT = "MU";
        public const string ROOM_STATE = "RS";
        public const string CLIENT_LIST = "CL";
        public const string CLIENT_STATE = "ST";
        public const string MILEAGE = "ML";
        public const string GPS = "GPS";
        public const string USER_NAME = "UN";
        public const string RESULT = "RT";
        public const string COMMAND = "CMD";

        // Packet Type
        public const char PACKET_MESSAGE = 'M';
        public const char PACKET_VIDEO = 'V';
        public const char PACKET_AUDIO = 'A';

        /* A packet is composed by {PACKET_TYPE} + '?' + 'CMD={CMD_TYPE}' + ({TAG_TYPE}={TAG_VALUE})* + optional ('&' + {Type specific payload})
         * 
         * Ex : Join message
         * M?CMD=JOIN&UN=xxxx&CIP=192.168.0.1
         * 
         * Ex : Update webcam video 
         * V?CMD=UV&CIP={Client ip}&{video payload}
         * 
         * Ex : Update audio
         * A?CMD=UA&CIP={Client ip}&{audio payload}
         */

        // Client states
        public const string CLIENT_STATE_NOT_READY = "NOTREADY";
        public const string CLIENT_STATE_READY = "READY";
        public const string CLIENT_STATE_LOADING = "LOADING";
        public const string CLIENT_STATE_LOADED = "LOADED";
        public const string CLIENT_STATE_START = "START";
        public const string CLIENT_STATE_FINISH = "FINISH";

        // Callback types
        public delegate void ReceiveMsgHandlers(Dictionary<string, string> info);
        public delegate void JoinRoomHandlers(string name, string ip);
        public delegate void LeaveRoomHandlers(string ip);
        public delegate void ClientStateChangeHandlers(string ip, float mileage, string state, string gps);
        public delegate void QuitRoomHandlers();
        public delegate void GameStartHandlers();
        public delegate void StartPlayHandlers();
        public delegate void RoomInfoUpdateHandlers(RVRoom room);

        // Game Modes
        public const string GAME_MODE_NORMAL = "NORMAL";
        public const string GAME_MODE_MILEAGE = "MILEAGE";
        public const string GAME_MODE_TIME = "TIME";

        // Room state
        public const string ROOM_STATE_OPEN = "OPEN";
        public const string ROOM_STATE_PLAYING = "PLAYING";
    }
}
